<?php
if (!defined('W_ROOT')) {
    exit();
}
$_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI'];
$pharmacy_id = $_SESSION['acc_id'];
$qstring = '';
$rowsPerPage = isset($_GET['num']) ? $_GET['num'] : '10';

if ((isset($_GET['catId']) && (int) $_GET['catId'] > 0)) {
    $changeCate = $_GET['catId'];
    $sql2 = "AND pic.category_id = $changeCate";
    $qstring = "catId=$changeCate";
} else {
    $changeCate = 0;
    $sql2 = '';
    $qstring = '';
}
$categoryList = buildCategoryOptions($changeCate);
$sql = "SELECT p.*,pip.*,pic.*, COUNT(pip.country_id) AS num_count FROM tblproduct AS p,tblproduct_in_pharmacy AS pip, tblproduct_in_category AS pic WHERE p.id = pip.product_id AND pip.pharmacy_id = $pharmacy_id AND pic.product_id = pip.product_id $sql2 GROUP BY pip.product_id ORDER BY pip.pid DESC";
$result = mysql_query(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage);
$row_num = mysql_num_rows($result);
?>
<form action="#" method="post" name="frmlist">
    <div>    
        <div class="title">Product List</div>
        <div style="float:left; padding-top:15px;">
            <select name="cboCategory" id="cboCategory" onChange="viewProduct();">
                <option value="0">View By Category</option>
                <?php echo $categoryList; ?>
            </select>
        </div>
        <div style="float:right; padding-top:15px;">
            <input type="button" name="btnadd" value="Add New" onClick="javascript:window.location.href='index.php?view=add&amp'; return false;" />&nbsp;&nbsp;
            <input type="button" name="btnpublish" value="Publish" onclick="publishSelected(document.frmlist, '<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>','No item is selected for Publishing!',1,'Publish(s)','process')" />&nbsp;&nbsp;
            <input type="button" name="btnunpublish" value="Unpublish" onclick="publishSelected(document.frmlist, '<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>','No item is selected for Unpublishing!',0,'Publish(s)','process')" />
        </div>
    </div>
    <div style="clear:both;">&nbsp;</div>
    <div class="title1">
        <table cellpadding="0" cellspacing="0" border="0" width="100%">
            <tr>
                <td width="3%" align="center" ><input type="checkbox" name="chkall" id="chkall" onClick="checkAll(this.form,this.checked)" style="cursor:pointer;"></td>
                <th align="center" width="17%">Image</th>
                <th align="left" style="padding-left:15px;">Product Name</th>
                <th width="19%">Price</th>
                <th width="10%">Status</th>
                <th width="12%">Other</th>
            </tr>
        </table>
    </div>
    <div class="center">
        <table cellpadding="0" cellspacing="0" border="0" width="100%">
            <?php
            $i = 1;
            if (dbNumRows($result) > 0) {
                while ($row = mysql_fetch_assoc($result)) {
                    extract($row);

                    if ($i % 2) {
                        $class = 'row1';
                    } else {
                        $class = 'row2';
                    }

                    $sql_img = "SELECT thumbnail FROM tblimage WHERE product_id = $id";
                    $result_img = mysql_query($sql_img);
                    $row_img = mysql_fetch_assoc($result_img);

                    if ($row_img['thumbnail'] && file_exists('images/' . $row_img['thumbnail'])) {
                        $thumbnail_w = 'images/' . $row_img['thumbnail'];
                        $thumbnail_s = 'images/' . $row_img['thumbnail'];
                    } else {
                        $thumbnail_w = 'images/no-image-small.png';
                        $thumbnail_s = 'images/no-image-small.png';
                    }
                    //Resample original image to display as Thumtail
                    $thumsize1 = getimagesize($thumbnail_s);
                    $percent1 = 75 / $thumsize1[0];

                    $country = mysql_query("SELECT COUNT(DISTINCT country_id) AS pid
		FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id);

                    $price = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price ASC LIMIT 0,1");
                    $myprice = mysql_fetch_assoc($price);
                    $price1 = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price DESC LIMIT 0,1");
                    $myprice1 = mysql_fetch_assoc($price1);
                    ?>
                    <tr class="<?php echo $class; ?>">
                        <td width="3%" align="center" style="padding-left:10px;"><input type="checkbox" name="id[]" value="<?php echo $id; ?>"/></td>
                        <td width="17%" align="center"><img src="<?php echo $thumbnail_w; ?>" width="<?php echo ($thumsize1[0] > 75) ? ($thumsize1[0] * $percent1) : $thumsize1[0] ?>" height="<?php echo ($thumsize1[0] > 75) ? ($thumsize1[1] * $percent1) : $thumsize1[1] ?>" border="0" /></td>
                        <td align="left" style="padding-left:15px;">
                            <strong style="color:#06C;"><?php echo stripslashes($product_name) ?></strong><br />
                            <em style="color:#666;">( Made in 
                                <?php
                                if ($num_count > 1) {
                                    echo $num_count . " Countries";
                                } else {
                                    echo getCountryName($country_id);
                                }
                                ?>
                                )</em>
                        </td>
                        <td width="19%" align="center"><strong style="color:#F60">
                                <?php
                                if ($myprice['price'] == $myprice1['price']) {
                                    echo '$' . $myprice['price'];
                                } else {
                                    echo '$' . $myprice['price'] . ' - $' . $myprice1['price'];
                                }
                                ?>
                            </strong></td>
                        <td width="10%" align="center"><?php echo stateProduct($id, $pharmacy_id, $status) ?></td>
                        <td width="12%" align="center">
        <?php
        if ($owner == $pharmacy_id) {
            ?>
                                <a href="index.php?view=edit&id=<?php echo $id ?>" style="font-size:10px; color:#06C;">Edit</a>
                                <?php
                            } else {
                                ?>
                                <a href="index.php?view=editexist&id=<?php echo $id ?>&owner=0" style="font-size:10px; color:#F30;">Edit</a>
                                <?php
                            }
                            ?>
                        </td>
                    </tr>        
        <?php
        $i++;
    }// end while
} else {
    ?>
                <tr>
                    <td colspan="6" style="height:75px;" align="center"><strong>You don't have any product yet! Please add new product</strong></td>
                </tr>
    <?php
}
?>        
        </table>

    </div>
    <div class="title1" style="background:#06F;">
        <div style="float:left; padding-top:2px;">Page: <?php echo $pagingLink; ?></div>
        <div style="float:right; padding-right:10px;">view per page
            <select name="numpage" onchange="javascript:viewByNumPage();">
                <option value="10" <?php echo($rowsPerPage == 10) ? ' selected="selected"' : ''; ?>>10</option>
                <option value="20" <?php echo($rowsPerPage == 20) ? ' selected="selected"' : ''; ?>>20</option>
                <option value="30" <?php echo($rowsPerPage == 30) ? ' selected="selected"' : ''; ?>>30</option>
                <option value="40" <?php echo($rowsPerPage == 40) ? ' selected="selected"' : ''; ?>>40</option>                                
                <option value="50" <?php echo($rowsPerPage == 50) ? ' selected="selected"' : ''; ?>>50</option>        
            </select></div>
    </div>
</form>